package com.simtop.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.User;
import com.simtop.service.SwlaneService;
import com.simtop.util.JwtUtil;
import com.simtop.util.convert.Convert;
import com.simtop.vo.Swdevice;
import com.simtop.vo.Swlane;
import com.simtop.vo.Swrelationlane;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("swlane")
@CrossOrigin
public class SwlaneController {
    @Autowired
    private SwlaneService service;
    @PostMapping("findall")
    @ResponseBody
    public ServerResponse findall(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
            List<Swlane> swlaneList = service.findall();
            return ServerResponse.createBySuccess("获取内容成功",swlaneList);
        }
    @PostMapping("savestate")
    @ResponseBody
    public ServerResponse savestate(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer swlanestate = jsonObject.getInteger("swlanestate");
        Integer id = jsonObject.getInteger("id");
        if(swlanestate != null && id != null) {
            service.savestate(swlanestate,id);
            return ServerResponse.createBySuccess("修改内容成功");
        }
        return ServerResponse.createBySuccess("修改内容失败");
    }
    @PostMapping("savename")
    @ResponseBody
    public ServerResponse savename(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject = JSONObject.parseObject(data);
        String swlane = jsonObject.getString("swlane");
        Integer id = jsonObject.getInteger("id");
        if(swlane != null && id != null) {
            service.savename(id,swlane);
            return ServerResponse.createBySuccess("修改内容成功");
        }
        return ServerResponse.createBySuccess("修改内容失败");
    }
    @PostMapping("deletelane")
    @ResponseBody
    public ServerResponse deletelane(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer id = jsonObject.getInteger("id");
        if(  id != null) {
            service.deletelane(id);
            return ServerResponse.createBySuccess("删除成功");
        }
        return ServerResponse.createBySuccess("删除失败");
    }
    @PostMapping("insertlane")
    @ResponseBody
    public ServerResponse insertlane(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject = JSONObject.parseObject(data);
        String swlane = jsonObject.getString("swlane");
        if(  swlane != null) {
            service.insertlane(swlane);
            return ServerResponse.createBySuccess("添加成功");
        }
        return ServerResponse.createBySuccess("添加失败");
    }
    @PostMapping("insertrelation")
    @ResponseBody
    public ServerResponse insertrelation(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer deviceid=jsonObject.getInteger("deviceid");
        service.deleterelation(deviceid);
        String devicelist = jsonObject.getString("devicelist");
        if (!devicelist.equals("")){
        String[] split = devicelist.split(",");
        Iterator<String> iterator = Arrays.stream(split).iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            Integer i = Integer.valueOf(next);
            service.insertrelation(deviceid,i);
        }
            return ServerResponse.createBySuccess("添加成功");
        }else {
            return ServerResponse.createBySuccess("清除成功");
        }

    }
    /**
     * 查询/选项查询
     * */
    @PostMapping("findlane")
    @ResponseBody
    public ServerResponse findlane(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer deviceid=jsonObject.getInteger("deviceid");
            List<Swrelationlane> swlaneList = service.findlane(deviceid);
            return ServerResponse.createBySuccess("获取内容成功",swlaneList);
        }
        catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }
    }
